/**
 * 逻辑控制器
 * 
 */
class LogicController {

    /**
     * 简单控制器
     * 
     * @param {XMLBuilder} parent 
     * @param {{testname: String, comments: String}} config 
     * @returns {XMLBuilder}
     */
    createSimpleController(parent, config = { testname: "Parallel Controlle", comments: "" }) {
        const { testname, comments } = config;
        const jmxController = parent.ele("GenericController", { guiclass: "LogicControllerGui", testclass: "GenericController", testname: testname });
        jmxController.ele("stringProp", { name: "TestPlan.comments" }).txt(comments);
        const jmxControllerHashTree = jmxController.up().ele("hashTree");
        return jmxControllerHashTree
    }

    /**
     * 事务控制器
     * 
     * @param {XMLBuilder} parent 
     * @param {{testname: String, comments: String, generateParentSample: boolean}} config 
     * @returns {XMLBuilder}
     */
    createTransactionController(parent, config = { testname: "Parallel Controlle", comments: "", generateParentSample: true }) {
        const { testname, comments, generateParentSample } = config;
        const jmxController = parent.ele("TransactionController", { guiclass: "TransactionControllerGui", testclass: "TransactionController", testname: testname });
        jmxController.ele("stringProp", { name: "TestPlan.comments" }).txt(comments).up()
            .ele("boolProp", { name: "TransactionController.includeTimers" }).txt("false").up()
            .ele("boolProp", { name: "TransactionController.parent" }).txt(generateParentSample);
        const jmxControllerHashTree = jmxController.up().ele("hashTree");
        return jmxControllerHashTree
    }

    /**
     * IF控制器
     * 
     * @param {XMLBuilder} parent 
     * @param {{testname: String, comments: String,condition}} config 
     * @returns {XMLBuilder}
     */
    createIfController(parent, config = { testname: "If Controller", comments: "" }) {
        const { testname, comments, condition } = config;
        const jmxController = parent.ele("IfController", { guiclass: "IfControllerPanel", testname: testname });
        jmxController.ele("stringProp", { name: "IfController.condition" }).txt(condition).up()
            .ele("boolProp", { name: "IfController.evaluateAll" }).txt("false").up()
            .ele("boolProp", { name: "IfController.useExpression" }).txt("true").up()
            .ele("stringProp", { name: "TestPlan.comments" }).txt(comments);
        const jmxControllerHashTree = jmxController.up().ele("hashTree");
        return jmxControllerHashTree
    }

    /**
     * 并行控制器
     * 
     * @param {XMLBuilder} parent 
     * @param {{testname: String, comments: String}} config 
     * @returns {XMLBuilder}
     */
    createParallelController(parent, config = { testname: "Parallel Controlle", comments: "" }) {
        const { testname, comments } = config;
        const jmxController = parent.ele("com.blazemeter.jmeter.controller.ParallelSampler", {
            guiclass: "com.blazemeter.jmeter.controller.ParallelControllerGui",
            testclass: "com.blazemeter.jmeter.controller.ParallelSamplerr",
            testname: testname
        });
        jmxController.ele("intProp", { name: "MAX_THREAD_NUMBER" }).txt("6").up()
            .ele("boolProp", { name: "PARENT_SAMPLE" }).txt("true").up()
            .ele("boolProp", { name: "LIMIT_MAX_THREAD_NUMBER" }).txt("false").up()
            .ele("stringProp", { name: "TestPlan.comments" }).txt(comments);
        const jmxControllerHashTree = jmxController.up().ele("hashTree");
        return jmxControllerHashTree;
    }

}

module.exports = LogicController;

